package com.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.sys.dao.DictItemMapper;
import com.sys.domain.DictItem;
import com.sys.domain.DictItemExample;
import com.sys.domain.DictItemExample.Criteria;
import com.sys.result.PageParam;
import com.sys.result.RequestPageVo;
import com.sys.result.RequestResultVo;
import com.sys.service.DictItemService;

@Service("dictItemService")
public class DictItemServiceImpl implements DictItemService {
	@Resource
	private DictItemMapper dictItemMapper;
	
	@Override
	public RequestPageVo<DictItem> queryDictItemList(PageParam param,String itemName,String dictCode) {
		RequestPageVo<DictItem> pageVo=new RequestPageVo<DictItem>();
		
		DictItemExample example=new DictItemExample();
		Criteria criteria=example.createCriteria();
		example.setOrderByClause("item_order");
		
		if(!StringUtils.isEmpty(itemName)){
			criteria.andItemNameLike("%"+itemName+"%");//根据字典值名称查询
		}
		criteria.andDictCodeEqualTo(dictCode);
		
		int count=dictItemMapper.countByExample(example);
		if(count>0){
			example.setPageSize(param.getLimit());
			example.setStartPage(param.getOffset());
			List<DictItem> dictList=dictItemMapper.selectByExample(example);//字典项列表
			pageVo.setLimit(param.getLimit());
			pageVo.setTotal(count);
			pageVo.setOffset(param.getOffset());
			
			pageVo.setRows(dictList);
		}
		
		return pageVo;
	}
	
	@Override
	public List<DictItem> queryDictItemByDictCode(String dictCode){
		List<DictItem> dictItemList=new ArrayList<DictItem>();
		DictItemExample example=new DictItemExample();
		example.setOrderByClause("item_order");
		Criteria criteria=example.createCriteria();
		if(!StringUtils.isEmpty(dictCode)){
			
			criteria.andDictCodeEqualTo(dictCode);
			dictItemList=dictItemMapper.selectByExample(example);
		}
		return dictItemList;
	}

	@Override
	public RequestResultVo<DictItem> addDictItem(DictItem dictItem) {
		RequestResultVo<DictItem> result=new RequestResultVo<DictItem>();
		try {
			dictItemMapper.insertSelective(dictItem);
			result.setSuccess(true);
			
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("添加失败!");
		}
		return result;
	}

	@Override
	public RequestResultVo<DictItem> updateDictItem(DictItem dictItem) {
		RequestResultVo<DictItem> result=new RequestResultVo<DictItem>();
		try {
			dictItemMapper.updateByPrimaryKeySelective(dictItem);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("更新失败!");
		}
		return result;
	}

	@Override
	public RequestResultVo<DictItem> deleteDictItem(Integer id) {
		RequestResultVo<DictItem> result=new RequestResultVo<DictItem>();
		try {
			dictItemMapper.deleteByPrimaryKey(id);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("删除失败!");
		}
		return result;
	}

}









